package javaer.starter.idea.window.component;

import com.intellij.psi.PsiFile;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author yangzhen
 * @date 2023/7/4 17:42
 * @since 1.0.0
 */
public class TemplateTable {

    public static final String[] fields = {"文件名称", "存在依赖时生效", "文件路径"};

    public static final Map<String, TemplateTable> artifactId_templateTable = new HashMap<>();

    private JScrollPane scrollPane;
    private JTable jTable;

    public TemplateTable(String artifactId, List<PsiFile> psiFiles, Set<String> dependencyIdSet) {
        DefaultTableModel model = new DefaultTableModel();
        for (String field : fields) {
            model.addColumn(field);
        }
        this.jTable = new JTable(model);
        scrollPane = new JScrollPane(this.jTable);
        artifactId_templateTable.put(artifactId, this);
        initTemplateTableData(psiFiles, dependencyIdSet);
    }

    public static String getDepId(String moduleId, String filename) {
        TemplateTable templateTable = artifactId_templateTable.get(moduleId);
        if (templateTable != null) {
            JTable jTable = templateTable.jTable;
            DefaultTableModel model = (DefaultTableModel) jTable.getModel();
            int rowCount = model.getRowCount();
            for (int i = 0; i < rowCount; i++) {
                String tableFilename = (String) model.getValueAt(i, 0);
                if (filename.equals(tableFilename)) {
                    return (String) model.getValueAt(i, 1);
                }
            }
        }
        return "";
    }

    private DefaultCellEditor getCellEditor(Set<String> dependencyIds) {
        DefaultComboBoxModel<String> comboBoxModel = new DefaultComboBoxModel<>();
        comboBoxModel.addElement("");
        for (String dependencyId : dependencyIds) {
            comboBoxModel.addElement(dependencyId);
        }

        JComboBox<String> comboBox = new JComboBox<>(comboBoxModel);
        DefaultCellEditor cellEditor = new DefaultCellEditor(comboBox);
        return cellEditor;
    }

    /**
     * 初始化模块table数据
     */
    public void initTemplateTableData(List<PsiFile> psiFiles, Set<String> dependencyIdSet) {
        DefaultTableModel model = (DefaultTableModel) jTable.getModel();
        model.setRowCount(0);
        jTable.getColumnModel().getColumn(1).setCellEditor(getCellEditor(dependencyIdSet));
        int index = 0;
        for (PsiFile psiFile : psiFiles) {
            Object[] columnValue = new Object[3];
            columnValue[0] = psiFile.getName();
            columnValue[2] = psiFile.getVirtualFile().getPath();
            DefaultCellEditor isDep = (DefaultCellEditor) jTable.getCellEditor(index, 1);
            ((JComboBox) isDep.getComponent()).setSelectedItem("");
            columnValue[1] = "";
            model.addRow(columnValue);
            index++;
        }
    }


    public JTable getTable() {
        return this.jTable;
    }

    public JScrollPane getScrollPane() {
        return this.scrollPane;
    }
}
